package com.wyg.ep_timer.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyg.ep_timer.entity.User;
import com.wyg.ep_timer.entity.UserEntity;
import com.wyg.ep_timer.mapper.UserEntityMapper;
import com.wyg.ep_timer.mapper.UserMapper;
import com.wyg.epkafkastarter.msg.MailMsg;
import com.wyg.epkafkastarter.producer.MailProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: WangYongGan
 * @Date: 2022/5/17 3:06
 * @Description:
 */
@Configuration
@EnableScheduling
@Slf4j
public class MailTask {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserEntityMapper userEntityMapper;
    MailProducer mailProducer;

    {
        mailProducer = MailProducer.getInstance();
    }

    @Scheduled(cron = "0 0 3 * * ?")
    public void mailTask() {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //查到所有的user
        final List<User> users = userMapper.selectList(wrapper.select("id", "email", "mail_flag", "nickname"));
        synchronized (Resource.lock) {
            QueryWrapper<UserEntity> userEntityWrapper = new QueryWrapper<>();
            //对每个user遍历
            users.forEach(user -> {
                if (user.isMailFlag()) {
                    //找到user的收藏
                    userEntityWrapper.clear();
                    userEntityWrapper.select("entity").eq("uid", user.getId());
                    final List<UserEntity> userEntities = userEntityMapper.selectList(userEntityWrapper);
                    boolean sendFlag = false;//日报格式
                    //新格式
                    List<String> entitiList = new ArrayList<>();
                    for (UserEntity userEntity : userEntities) {
                        if (Resource.cache.get(userEntity.getEntity()) != null) {
                            entitiList.add(userEntity.getEntity());
                        }
                    }

                    if (entitiList.size() > 0) {
                        mailProducer.send(MailMsg.builder().to(user.getEmail()).entities(entitiList).build());
                    }
                    //老版本格式
//                    StringBuilder sb = new StringBuilder();
//                    sb.append(user.getEmail());
//                    sb.append(",");
//                    for (UserEntity entity : userEntities) {
//                        Integer num = (Integer) Resource.cache.get(entity);
//                        if (num != null) {
//                            sendFlag = true;
//                            sb.append(entity);
//                            sb.append("::");
//                            sb.append(num);
//                            sb.append(",");
//                        }
//                    }
//                    if (sendFlag) {
//                        MailTopic.sendMailKafka(sb.toString());
//                    }
                }
            });
        }
        //每天 清除
        Resource.cache.clear();
    }
}
